<template>
  <div>
    <van-nav-bar title="标题" left-arrow @click-left="$router.go(-1)" />
    <div class="photo">
      <van-image round fit="cover" :src="userInfo.photo" />
      <van-uploader :after-read="afterRead" />
    </div>
    <van-cell-group>
      <van-cell
        title="名称"
        is-link
        :value="userInfo.name"
        @click="showUserName"
      />
      <van-cell
        title="性别"
        is-link
        :value="userInfo.gender ? '女' : '男'"
        @click="showSex = true"
      />
      <van-cell
        title="生日"
        is-link
        :value="userInfo.birthday"
        @click="showDate = true"
      />
    </van-cell-group>

    <van-dialog
      v-model="showChangeName"
      title="修改姓名"
      show-cancel-button
      @confirm="changeName"
    >
      <van-field v-model="newName" placeholder="请输入姓名" ref="name" />
    </van-dialog>
    <van-popup v-model="showSex" position="bottom">
      <van-nav-bar title="修改性别" left-arrow @click-left="showSex = false" />
      <van-cell title="男" is-link @click="changeSex(0)" />
      <van-cell title="女" is-link @click="changeSex(1)" />
    </van-popup>
    <van-popup v-model="showDate" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :max-date="maxDate"
        :min-date="minDate"
        @confirm="setBirthday"
      />
    </van-popup>

    <!-- vue-cropper -->
    <div class="cropper" v-show="showCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        fixed
        autoCropWidth="120"
        autoCropHeight="120"
        autoCrop
        mode
      ></vueCropper>
      <van-button type="primary" @click="showCropper = false">取消</van-button>
      <van-button type="info" @click="submitPhoto">确认</van-button>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { changeUserInfo } from './user.js'
// import moment from 'moment'
import { myDate } from './date.js'
import { setIcon } from './user.js'
import { VueCropper } from 'vue-cropper'
export default {
  name: 'edit',
  components: { VueCropper }, //注册组件
  data () {
    return {
      showChangeName: false,
      newName: '',
      showSex: false,
      showDate: false,
      currentDate: new Date(),
      maxDate: new Date(2021, 7, 16),
      minDate: new Date(1980, 7, 16),
      img: '',
      showCropper: false
    }
  },
  methods: {
    showUserName () {
      this.showChangeName = true
      this.newName = this.userInfo.name
      this.$nextTick(() => {
        this.$refs.name.focus()
      })
    },
    async changeName () {
      let res = await changeUserInfo({ name: this.newName })
      this.$toast.success('修改成功')

      this.$store.commit('setUserInfo', {
        ...this.userInfo, // 平铺开来全部传参
        name: res.data.data.name // 只传一个会覆盖
      })
    },
    changeSex (gender) {
      changeUserInfo({ gender }).then(res => {
        console.log(res)
        this.$toast.success('修改成功')
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          gender: res.data.data.id == 1 ? 1 : 0
        })
        this.showSex = false
      })
    },
    setBirthday () {
      // const birthday = moment(this.currentDate).format('YYYY-MM-DD')
      let birthday = myDate(this.currentDate)
      changeUserInfo({ birthday }).then(res => {
        console.log(res)
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          birthday: res.data.data.birthday
        })
        this.showDate = false
      })
    },
    afterRead (file) {
      // 此时可以自行将文件上传至服务器
      this.img = file.content
      this.showCropper = true
      //无截图框：
      // setIcon(fd).then(res => {
      //   this.$toast.success('修改成功')
      //   this.$store.commit('setUserInfo', {
      //     ...this.userInfo,
      //     photo: res.data.data.photo
      //   })
      // })
    },
    submitPhoto () {
      // 获取截图的blob数据
      this.$refs.cropper.getCropBlob(data => {
        let fd = new FormData()
        fd.append('photo', data)
        setIcon(fd).then(res => {
          this.$store.commit('setUserInfo', {
            ...this.userInfo,
            photo: res.data.data.photo
          })
          this.showCropper = false
        })
      })
    }
  },
  computed: {
    ...mapState(['userInfo'])
  }
}
</script>
<style lang="less">
.van-nav-bar {
  .van-nav-bar__content {
    background-color: #3296fa;
    .van-icon-arrow-left {
      color: white;
    }
    .van-nav-bar__title {
      color: white;
    }
  }
}
.photo {
  text-align: center;
  margin: 20px 0;
  .van-image__img {
    width: 120px;
    height: 120px;
  }
  .van-uploader {
    opacity: 0;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    width: 120px;
    height: 120px;
    .van-uploader__upload {
      width: 120px;
      height: 120px;
    }
  }
}
.cropper {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  .van-button--primary {
    position: absolute;
    left: 0;
    bottom: 0;
  }
  .van-button--info {
    position: absolute;
    right: 0;
    bottom: 0;
  }
}
</style>
